<style lang="less">
    @import "../../../styles/table.less";
</style>
<template>
    <div class="search">
        <Row type="flex" justify="space-between" class="code-row-bg">
            <Col>
                <Card>
                    <Row>
                        <Form ref="searchForm" :model="searchForm" inline :label-width="70" class="search-form">
                            <Form-item label="课程名称" prop="courseTitle">
                                <Input type="text" v-model="searchForm.courseTitle" placeholder="请输入" clearable style="width: 200px"/>
                            </Form-item>
                            <Form-item label="课程状态" prop="courseStatus">
                                <Select v-model="searchForm.courseStatus" placeholder="请选择" clearable style="width:100px">
                                    <Option value="1">正常</Option>
                                    <Option value="0">已下架</Option>
                                </Select>
                            </Form-item>
                            <Form-item style="margin-left:-35px;" class="br">
                                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                                <Button @click="handleReset">重置</Button>
                            </Form-item>
                        </Form>
                    </Row>
                    <div class="left-operation-right">
                        <div>
                            <Button @click="addCourse" type="primary" icon="md-add">添加</Button>
                            <Button @click="getDataList" icon="md-refresh">刷新</Button>
                            <circleLoading v-if="operationLoading"/>
                        </div>
                    </div>
                    <div v-if="showType=='list'">
                        <Row>
                            <Table :loading="loading" border :columns="columns" :data="data" sortable="custom" @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table"></Table>
                        </Row>
                    </div>

                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
        <Modal v-model="picVisible" title="封面图" draggable>
            <img :src="fileUrl" alt="无效的图片链接" style="width: 100%;margin: 0 auto;display: block;">
        </Modal>
        <course-drawer ref="courseDrawer"></course-drawer>
        <catlog-drawer ref="catlogDrawer"></catlog-drawer>
    </div>
</template>

<script>
    import {courseContinueList, setCourseStatus} from "@/api/bus";
    import circleLoading from "../../my-components/circle-loading.vue";
    import catlogDrawer from "./catlog-drawer/catlogDrawer";
    import courseDrawer from "./course-drawer";

    export default {
        name: "courseManage",
        components: {
            courseDrawer,
            circleLoading,
            catlogDrawer
        },
        data() {
            return {
                loading: false, // 表加载状态
                editTitle: "", // 编辑节点名称
                searchKey: "", // 搜索树
                showType: "list",//数据展示方式
                selectNode: {},
                operationLoading: false, // 操作加载状态
                selectCount: 0, // 多选计数
                selectList: [], // 多选数据
                fileUrl: "",
                picVisible: false,
                searchForm: {
                    // 搜索框对应data对象
                    courseTitle: "",
                    courseStatus: -1,
                    indextagId: -1,
                    courseTypeId: "",
                    pageNumber: 1, // 当前页数
                    pageSize: 10, // 页面大小
                    sort: "c.createTime", // 默认排序字段
                    order: "desc" // 默认排序方式
                },
                form: {
                    // 添加或编辑表单对象初始化数据
                    sex: 1,
                    parentId: 0,
                    type: 0
                },
                columns: [
                    {
                        type: "index",
                        width: 60,
                        align: "center",
                    },
                    {
                        title: "封面图",
                        key: "courseImage",
                        width: 120,
                        align: "center",
                        render: (h, params) => {
                            return h("img", {
                                attrs: {
                                    src: params.row.courseImage,
                                    alt: "加载图片失败"
                                },
                                style: {
                                    cursor: "pointer",
                                    width: "80px",
                                    height: "60px",
                                    margin: "3px 0",
                                    "object-fit": "contain"
                                },
                                on: {
                                    click: () => {
                                        this.showPic(params.row.courseImage);
                                    }
                                }
                            });

                        }
                    },
                    {
                        title: "课程名称",
                        align: "center",
                        key: "courseTitle",
                        tooltip: true,
                    },
                    {
                        title: "分类名称",
                        align: "center",
                        width: 130,
                        key: "typeName",
                        render: (h, params) => {
                            if (params.row.typeName) {
                                return h("div", params.row.typeName);
                            } else {
                                return h("div", "--");
                            }
                        }
                    },
                    {
                        title: "讲师",
                        align: "center",
                        width: 130,
                        key: "userName",
                    },
                    {
                        title: "上架",
                        key: "courseStatus",
                        width: 100,
                        align: "center",
                        render: (h, params) => {
                            return h(
                                "i-switch",
                                {
                                    props: {
                                        value: params.row.courseStatus,
                                        size: "large",
                                        "true-value": 1,
                                        "false-value": 0
                                    },
                                    on: {
                                        "on-change": v => {
                                            this.setCourseStatus(params.row, v);
                                        }
                                    }
                                },
                                [
                                    h("span", {slot: "open"}, "是"),
                                    h("span", {slot: "close"}, "否")
                                ]
                            );
                        }
                    },
                    {
                        title: "创建时间",
                        key: "createTime",
                        width: 150,
                        align: "center",
                        sortType: "desc"
                    },
                    {
                        title: "操作",
                        key: "action",
                        width: 220,
                        align: "center",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "warning",
                                            size: "small"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.$refs.catlogDrawer.show(params.row);
                                            }
                                        }
                                    },
                                    "课程目录"
                                ), h(
                                    "Button",
                                    {
                                        props: {
                                            type: "primary",
                                            size: "small"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    "编辑"
                                ),
                            ]);
                        }
                    }
                ],
                data: [], //表单数据
                total: 0// 表单数据总数
            };
        },
        methods: {
            init() {
                this.getDataList();
            },
            showPic(v) {
                this.fileUrl = v;
                this.picVisible = true;
            },
            //添加课程
            addCourse() {
                let query = {type: 0, courseTypeId: this.selectNode.id, backRoute: this.$route.name}
                this.$refs.courseDrawer.showCourseDrawer(query);
            },
            //设置课程推荐状态
            setCourseStatus(row, v) {
                let params = {
                    courseId: row.id,
                    courseStatus: v
                }
                setCourseStatus(params).then(res => {
                    if (res.status === true) {
                        this.$Message.success(res.info);
                    }
                });
                ;
            },
            //课程分类查询
            search() {
                let params = {}
                // 搜索树
                if (this.searchKey) {
                    params.title = this.searchKey;
                } else {
                    params.parentId = 0;
                    params.title = "";
                }
            },
            //课程分类选择事件
            selectTree(v) {
                if (v) {
                    for (let attr in v) {
                        if (v[attr] === null) {
                            v[attr] = "";
                        }
                    }
                    let str = JSON.stringify(v[0]);
                    let data = JSON.parse(str);
                    this.selectNode = data;
                    if (this.selectNode.parentId == "0") {
                        this.searchForm.courseTypeId = data.id;
                        this.form.parentId = data.id;
                    } else {
                        this.searchForm.courseTypeId = data.id;
                    }
                    this.editTitle = data.title;
                    // 重新加载表
                    this.getDataList();
                } else {
                    this.searchForm.courseTypeId = "";
                }
            },
            changeSelect(v) {
                this.selectCount = v.length;
                this.selectList = v;
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getDataList();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getDataList();
            },
            changeShowType() {
                this.searchForm.pageNumber = 1;
                if (this.showType == "list") {
                    this.searchForm.pageSize = 5;
                } else {
                    this.searchForm.pageSize = 12;
                }
                this.getDataList();
            },
            getDataList() {
                this.loading = true;
                courseContinueList(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.data = res.data.rows;
                        this.total = res.data.total;
                    }
                });
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getDataList();
            },
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                // 重新加载数据
                this.getDataList();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getDataList();
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            commentList(row) {
                let query = {courseId: row.id};
                this.$refs.coursecommentDrawer.showDrawer(query);
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            edit(v) {
                let query = {type: 1, courseId: v.id, backRoute: this.$route.name};
                this.$refs.courseDrawer.showCourseDrawer(query);
            },
        },
        mounted() {
            this.init();
        }
    };
</script>